# -*- coding: utf-8 -*-

#  Copyright (C) 2008 - Eugene Khorev
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330,
#  Boston, MA 02111-1307, USA.

import pygtk
pygtk.require("2.0")
import gtk
import gedit
import time
import os
import sys
import gettext
import config_dlg
import bookmarks
import window_helper

APP_NAME = "plugin"
LOC_PATH = os.path.join(os.path.expanduser("~/.gnome2/gedit/plugins/advanced-bookmarks/lang"))

gettext.find(APP_NAME, LOC_PATH)
gettext.install(APP_NAME, LOC_PATH, True)

class AdvancedBookmarksPlugin(gedit.Plugin):
    def __init__(self):
        gedit.Plugin.__init__(self)
        
        self._instances = {}

        # Create configuration dialog
        self._dlg_conf = config_dlg.conf_dlg(None)
        self._dlg_conf.connect("response", self._on_dlg_conf_response)
        
        # Get configuration dictionary
        self._config = self._dlg_conf.get_config()

        # Create bottom pane
        self._pane = gtk.ScrolledWindow()

        # Create bottom pane tree
        self._tree = gtk.TreeView()
        
        # Create line number column
        self._line_column = gtk.TreeViewColumn(_('Line'))
        self._tree.append_column(self._line_column)
        
        self._line_cell = gtk.CellRendererText()
        self._line_column.pack_start(self._line_cell, True)

        self._line_column.add_attribute(self._line_cell, 'text', 0)
        
        # Create comment column
        self._comment_column = gtk.TreeViewColumn(_('Comment / Content'))
        self._tree.append_column(self._comment_column)
        
        self._comment_cell = gtk.CellRendererText()
        self._comment_column.pack_start(self._comment_cell, True)

        self._comment_column.add_attribute(self._comment_cell, 'text', 1)
        
        # Addtitional settings
        self._line_column.set_sort_column_id(0)
        self._tree.set_enable_tree_lines(True)
        self._tree.set_search_column(1)
        self._tree.set_rules_hint(True)
        
        # Add tree to bottom pane
        self._pane.add_with_viewport(self._tree);
        self._tree.show()

        # Setup row selection event
        self._tree.connect("row-activated", self._on_row_activated)

        # Setup configuration file path
        conf_path = os.path.join(os.path.expanduser("~/.gnome2/gedit/plugins/"), "reopen-tabs/plugin.conf")
        
        # Create bookmark list
        self._bookmarks = bookmarks.bookmark_list(conf_path)
        
    def activate(self, window):
        # Create icon for bottom pane
        self._icon = gtk.Image()
        self._icon.set_from_icon_name('stock_help-add-bookmark', gtk.ICON_SIZE_MENU)

        # Add tree to bottom pane
        pane = window.get_bottom_panel()
        pane.add_item(self._pane, _('Bookmarks'), self._icon)

        # Create window helper for an instance
        self._instances[window] = window_helper.window_helper(self, window)
        
    def deactivate(self, window):
        self._instances[window].deactivate()
        del self._instances[window]
        
    def update_ui(self, window):
        self._instances[window].update_ui()
                        
    def create_configure_dialog(self): # Upadtes configuration dialog (executes by framework)
        # Update configuration dialog widgets
        self._dlg_conf.load_conf()
        self._dlg_conf.show_all()

        # Return configuration dialog to framework
        return self._dlg_conf

    def _on_dlg_conf_response(self, dlg_conf, res): # Handles configuration dialog response
        # Hide configuration dialog
        dlg_conf.hide()

        # Check if user pressed OK button
        if res == gtk.RESPONSE_OK:
            # Save configuration
            self._dlg_conf.write_config()
    
    def _on_row_activated(self, view, row, column):
        # Get bookmark line
        model = view.get_model()
        bookmark = model.get_iter(row)
        line = model.get_value(bookmark, 0)
        
        # Get active document
        app = gedit.app_get_default()
        win = app.get_active_window()
        doc = win.get_active_document()
        buf = win.get_active_view()
        
        # Jump to bookmark
        doc.goto_line(int(line)-1)
        buf.scroll_to_cursor()
        buf.grab_focus()
    
# ex:ts=4:et:
